﻿using System;
using System.Collections.Generic;
using System.Text;

using ET;
using Marx;
using Marx.Utils;

namespace Server
{
    [ObjectSystem]
    public class DailyRefreshAwakeSystem : AwakeSystem<DailyRefreshComponent, TimeSpan>
    {
        public override void Awake(DailyRefreshComponent self, TimeSpan timePoint)
        {
            self.Awake(timePoint);
        }
    }


    public class DailyRefreshComponent : Entity, IAwake<TimeSpan>, IDestroy
    {
        public TimeSpan RefreshTimePoint => refreshTimePoint_;
        private TimeSpan refreshTimePoint_;
        private RefreshTimer timer_;

        public void Awake(TimeSpan point)
        {
            refreshTimePoint_ = point;
            timer_ = new RefreshTimer();

            Start();
        }

        private void Start()
        {
            TimeSpan during = TimeUtils.GetRefreshTimeSpanFromNow(refreshTimePoint_);
            timer_.StartRefreshTimer(during,
                TimeSpan.FromDays(1),
                () => Game.EventSystem.Publish(new EventType.DailyRefresh() { Domain = this.DomainScene() })).Coroutine();
        }

        public override void Dispose()
        {
            if(IsDisposed)
            {
                return;
            }
            base.Dispose();
            timer_.Cancel();
        }
    }
}
